package main.stack;

import org.junit.jupiter.api.Test;

import java.util.Stack;

/**
 * 每日温度问题
 * @author: kejl
 * @create: 2020-11-08 13:45
 */
public class DailyTemperatures {

    @Test
    int[] baoLi(int[] T){
        int length = T.length;
        int[] result = new int[length];
        for(int i = 0;i<length;i++){
            for(int j=i+1;j<length;j++){
                if(T[i]<T[j]){
                    result[i] = j-i;
                    break;
                }
            }

        }
        return result;
    }


    public int[] dailyTemperatures(int[] T) {
            int[] result = new int[T.length];

            Stack<Integer> stack = new Stack<Integer>();

            for(int i = 0;i<T.length;i++){
                while(!stack.empty() && T[stack.peek()]<T[i]){
                    result[stack.peek()] = i - stack.peek();
                    stack.pop();
                }

                stack.push(i);

            }
            return result;
        }




}
